/*
 * Copyright By Peak3
 * Copyright By c336bf6ff03a756989bb456b5b5e3271228d43ed38461eb5cb5755d17a2fc9b8
 * All rights reserved.
 *
 * If you are not the intended user, you are hereby notified that any use, disclosure, copying, printing, forwarding or
 * dissemination of this property is strictly prohibited. If you have got this file in error, delete it from your system.
 */

package com.vv.tools.common.util;

import com.vv.tools.common.message.CustomerBizException;
import com.vv.tools.common.message.Result;
import com.vv.tools.common.message.ResultEnum;

import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import lombok.extern.slf4j.Slf4j;

/**
 * @author liwen
 */
@Slf4j
public class ResponseUtil {

    private ResponseUtil() {
    }

    public static <T> void of(HttpServletResponse response, Result<T> result) {
        response.setContentType("application/json;charset=UTF-8");
        String res = JsonUtil.toJsonString(result);
        assert res != null;
        try (ServletOutputStream out = response.getOutputStream()) {
            out.write(res.getBytes(StandardCharsets.UTF_8));
            out.flush();
        } catch (IOException e) {
            throw new CustomerBizException(ResultEnum.EXCEPTION);
        }
    }

}
